
	***************************************************
	* (1.3) Plot the results
	***************************************************
	
	insheet using "$d_data\CensusSRBs.csv", clear 

	twoway 	(connected srb_1990_88rs year, sort msymbol(smx) lcolor(black) mcolor(black))  ///
			(connected srb_1990_coalers year, sort msymbol(smtriangle) lcolor(black) mcolor(black))  ///
			(connected srb_1990_bannisterrs year, sort msymbol(smdiamond) lcolor(black) mcolor(black))  ///
			(connected srb_1990_jiangrs year, sort msymbol(smplus) lcolor(black) mcolor(black))  ///
			(connected srb_1988 year, sort msymbol(smcircle) lcolor(blue) mcolor(blue)) 	 ///
			if year<1985 & year>1966, legend(pos(6) title("1990 Population Census Mortality Adjusted Using Mortality Rates Derived From:", size(small)) order(1 "1988 Survey" 2 "Coale 1984" 3 "Bannister 1994" 4 "Jiang et al. 1984") size(small) cols(4)) ///
			ytitle("Sex Ratio At Birth") xtitle("Birth Year") yscale(range(104 110)) ylab(104(1)110) 		
	graph export "$d_fig\Fig_A3_SRB.jpg", replace

	twoway 	(connected srb_1982_88rs year, sort msymbol(smx) lcolor(black) mcolor(black))  ///
			(connected srb_1982_coalers year, sort msymbol(smtriangle) lcolor(black) mcolor(black))  ///
			(connected srb_1982_bannisterrs year, sort msymbol(smdiamond) lcolor(black) mcolor(black))  ///
			(connected srb_1982_jiangrs year, sort msymbol(smplus) lcolor(black) mcolor(black))  ///
			(connected srb_1988 year, sort msymbol(smcircle) lcolor(blue) mcolor(blue)) 	 ///
			if year<1982 & year>1974, legend(pos(6) title("Sex Ratios at Birth From the 1982 Population Census," "Mortality Adjusted Using Mortality Rates From:", size(medsmall)) order(1 "1988 Survey" 2 "Coale 1984" 3 "Bannister 1994" 4 "Jiang et al. 1984") size(small) rows(1)) ///
			ytitle("Sex Ratio At Birth") xtitle("Birth Year") yscale(range(104 110)) ylab(104(1)110) 	
	graph export "$d_fig\Fig_A4_SRB.jpg", replace

	  
	twoway 	(connected srb_1990_2 year, lcolor(red) lpattern(solid) lwidth(medthick) mcolor(red) mstyle(dot) msize(tiny) ) ///
			(connected srb_1988_2 year, lcolor(red) lpattern(dash) lwidth(medthick) mcolor(red) mstyle(dot)  msize(tiny)) ///
			(connected srb_1990_3 year, lcolor(blue) lpattern(solid) lwidth(medthick) mcolor(blue) mstyle(dot) msize(tiny)) ///
			(connected srb_1988_3 year, lcolor(blue) lpattern(dash) lwidth(medthick) mcolor(blue) mstyle(dot)  msize(tiny)) ///
			if year>=1977  & year<=1987, xtitle("Birth Year") xlab(1977(1)1985, angle(45)) ytitle("Sex Ratio at Birth") ///
			yline(106, lcolor(black)) legend(order(1 "Second Births; 1990 Census"  2 "Second Births, 1988 Survey" 3 "Third Births, 1990 Census" 4 "Third Births, 1988 Survey" ) ///
			pos(6) cols(2) size(small) span)  xline(0, lcolor(gs13))  ///
			title("Sex Ratio at Birth: Births with No Older Male Siblings") ysize(12) xsize(20) 
	graph export "$d_fig\Fig_A5_SRB.jpg", replace

*********************************************************************
* (2.0) Based on Coale 1984
*********************************************************************	
	clear 
	
	*First import and interpolate population by year and age from census data
	insheet using "$d_data\agestructure.csv"

	tsset age census 
	tsfill
	 ipolate male census, gen(male_ipolate)
	 ipolate female census, gen(female_ipolate)

	rename census year
	
	tempfile pop_ipolate
	save `pop_ipolate', replace

	* Panel of mother-year obs from the 2 per thousand survey with indicators for whether they delivered a child in each year 
	use 	"$f_2PKmotherpanel", clear	
	gen delivered_boy = delivered_son==1
	gen delivered_girl = delivered_son==0 & delivered==1
	
	rename current_age age 
	* Collapse to birth rates for each age group and each year
	collapse delivered delivered_boy delivered_girl, by(year age)

	* Bring in interpolated census population and generate estimates of the total number of births to women of each age in each year
	merge 1:1 year age using `pop_ipolate'

	gen births_girls 	= delivered_girl*female_ipolate 
	gen births_boys		= delivered_boy*male_ipolate

	* Now sum to get births per year
	collapse (sum) est_girls=births_girls (sum) est_boys=births_boys, by(year) 

	* Now age forward to 88
	gen age1982 = 1982-year
	gen age1990 = 1990-year

	reshape long age, i(year) j(census)
	rename year birthyear

	tempfile estpop
	save `estpop', replace

	clear
	insheet using "$d_data\agestructure.csv"
	keep if census == 1982 | census == 1990
	merge 1:1 age census using `estpop' 

	replace birthyear = 1982-age if census == 1982
	replace birthyear = 1990-age if census == 1990

	* Underreported boys and girls per 100 births
	gen underreport_rate_male = ((male-est_boys)/male)*100
	gen underreport_rate_female = ((female-est_girls)/female)*100

	gen SR_underreport = (male-est_boys)/(female-est_girls)
		
	gen differential_underreport =  underreport_rate_female 	- underreport_rate_male

	***************************************************
	* (2.1) Plot the results
	***************************************************
			
	twoway 	(connected differential_underreport birthyear if census == 1982 , msymbol(smx) lcolor(red) mcolor(red))  ///
			(connected differential_underreport birthyear  if census == 1990, msymbol(smx) lcolor(blue) mcolor(blue)) 	 ///
			if birthyear<1983 & birthyear>1966	, legend(pos(6) title("Comparison Census Year:", size(small)) order(1 "1982 Census" 2 "1990 Census" ) size(small) rows(1)) ///
			ytitle("Excess Unreported Females" "per 100 Births", size(small)) xtitle("Birth Year") title("Differential Underreporting of Female Births vs Male Births", size(medium)) ///
			yscale(range(0 10)) ylab(0(2)10) 
	graph export "$d_fig\Fig_A6_Underreporting.jpg", replace

	
log close
